﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Media.Effects;
using System.Collections;

namespace Calc3D
{
    public partial class MainPage : PhoneApplicationPage
    {

        PlaneProjection ppo = new PlaneProjection();
        Button BTN;
        bool isDetails = false;

        private string m_value = "";
        private List<string> m_store = new List<string>();

        Storyboard sb_ContAnimations = new Storyboard();
        bool Exitting = false;
        private void MainEntryAnimation()
        {
            // Creating the Scale Animation of the Main Grid
            Storyboard sbScaleMainGrid = new Storyboard();
            DoubleAnimation daMainGridScaleX = new DoubleAnimation();
            DoubleAnimation daMainGridScaleY = new DoubleAnimation();
            Storyboard.SetTarget(daMainGridScaleX, ContentGrid);
            Storyboard.SetTarget(daMainGridScaleY, ContentGrid);
            Storyboard.SetTargetProperty(daMainGridScaleX, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"));
            Storyboard.SetTargetProperty(daMainGridScaleY, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"));

            PlaneProjection ppd = new PlaneProjection();
            ContentGrid.Projection = ppd;
            DoubleAnimation animationYd = new DoubleAnimation()
            {
                From = 0,
                Duration = TimeSpan.FromMilliseconds(2000),
                To = 360,
            };
            Storyboard.SetTarget(animationYd, ppd);
            Storyboard.SetTargetProperty(animationYd, new PropertyPath("RotationY"));
            sbScaleMainGrid.Children.Add(animationYd);
            sbScaleMainGrid.Children.Add(daMainGridScaleX);
            sbScaleMainGrid.Children.Add(daMainGridScaleY);

            daMainGridScaleX.From = 0;
            daMainGridScaleX.To = 1;
            daMainGridScaleY.From = 0;
            daMainGridScaleY.To = 1;

            sbScaleMainGrid.Begin();
        }
        public MainPage()
        {
            InitializeComponent();

            SupportedOrientations = SupportedPageOrientation.Portrait | SupportedPageOrientation.Landscape;
        }

        private void checkBox1_Checked(object sender, RoutedEventArgs e)
        {

            PlaneProjection pp = (PlaneProjection)button1.Projection;
            //Storyboard _storyBoard = new Storyboard();
            sb_ContAnimations.Children.Clear();
            DoubleAnimation animationX = new DoubleAnimation()
            {
                From = -30,
                Duration = TimeSpan.FromSeconds(1),
                To = 30,
                AutoReverse= true,
                RepeatBehavior = RepeatBehavior.Forever
            };

            DoubleAnimation animationZ = new DoubleAnimation()
            {
                From = -5,
                Duration = TimeSpan.FromSeconds(1),
                To = 5,
                AutoReverse = true,
                RepeatBehavior = RepeatBehavior.Forever
            };

            Storyboard.SetTarget(animationX, pp);
            Storyboard.SetTargetProperty(animationX, new PropertyPath("RotationX"));
            sb_ContAnimations.Children.Add(animationX);
            Storyboard.SetTarget(animationZ, pp);
            Storyboard.SetTargetProperty(animationZ, new PropertyPath("RotationZ"));
            sb_ContAnimations.Children.Add(animationZ);
            sb_ContAnimations.Begin(); 


            
        }

        private void checkBox1_Unchecked(object sender, RoutedEventArgs e)
        {
            sb_ContAnimations.Stop();

            PlaneProjection pp = (PlaneProjection)button1.Projection;

            Storyboard _storyBoard = new Storyboard();

            DoubleAnimation animationX = new DoubleAnimation()
            {
                From = -60,
                Duration = TimeSpan.FromSeconds(1),
                To = -0,
            };

            Storyboard.SetTarget(animationX, pp);

            Storyboard.SetTargetProperty(animationX, new PropertyPath("RotationX"));

            _storyBoard.Children.Add(animationX);
            _storyBoard.Begin(); 
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            MainEntryAnimation();
            //PlaneProjection pp = new PlaneProjection();
            foreach (UIElement aButton in this.ContentGrid.Children)
            {
                if (aButton.GetType() == typeof(Button))
                {
                    ((Button)aButton).Click += new RoutedEventHandler(Button_Click);
                    aButton.Projection = ppo;
                    //aButton.Effect = new DropShadowEffect() {  Color= Colors.White};
                    //aButton.CacheMode = new BitmapCache();
                }
            }
        }


        private void AddOperatorToArray(Button bttn)
        {
            m_store.Add(m_value);
            textBlock1.Text += bttn.Content;
            m_value = "";
            m_store.Add(bttn.Content.ToString());
            //bttnDot.Enabled = true;
            //SetEnableOperatorBttns(false);
        }
        private void AddToArray(Button bttn)
        {
            m_value += bttn.Content;
            textBlock1.Text += bttn.Content;
            //SetEnableOperatorBttns(true);
        }

        private void Reset()
        {
            m_value = "";
            textBlock1.Text = "";
            m_store.Clear();
            //bttnDot.Enabled = true;
        }


        void Button_Click(object sender, RoutedEventArgs e)
        {
            Button CurrentButton = (Button)sender;

            if ((bool)checkBox2.IsChecked)
                AnimateButtonClick(CurrentButton);

            switch (CurrentButton.Content.ToString())
            {
                case "*":
                    
                    //DetailsText.Text += textBlock1.Text + " *" + System.Environment.NewLine;
                    AddOperatorToArray(CurrentButton);
                    break;
                case "/":
                    
                    //DetailsText.Text += textBlock1.Text + " /" + System.Environment.NewLine;
                    AddOperatorToArray(CurrentButton);
                    break;
                case "+":
                    
                    //DetailsText.Text += textBlock1.Text + " +" + System.Environment.NewLine;
                    AddOperatorToArray(CurrentButton);
                    break;
                case "-":
                    
                    //DetailsText.Text += textBlock1.Text + " -" + System.Environment.NewLine;
                    AddOperatorToArray(CurrentButton);
                    break;
                case ".":
                    AddToArray(CurrentButton);
                    break;

                case "=":

                    try
                    {
                        DetailsText.Text += textBlock1.Text + " = " +System.Environment.NewLine;
                        m_store.Add(m_value);
                        m_store.Add("=");

                        /* m_res here take the first element in m_store if this element is 
                         * operator like +*-/ Exception give us Error else it will take the
                         *  Digits or dot point 
                         */
                        float m_res = float.Parse(m_store[0].ToString());

                        /* this loop extract all element in Array then check it 
                         * if the element is operator it will calculate the prefix 
                         * and postfix and give us the result 
                         * Example 1+2/3*5 
                         * 1) 1+2 = 3
                         * 2) 3/3 = 1
                         * 3) 1*5  = 5
                         * note:- no periority operator and no brakets so it's basic calculator:)
                         */
                        for (int i = 0; i < m_store.Count; i++)
                        {

                            if (m_store[i].ToString() == "+")
                            {
                                textBlock1.Text = "";
                                m_res += float.Parse(m_store[i + 1].ToString());
                                textBlock1.Text = m_res.ToString();
                            }
                            else if (m_store[i].ToString() == "-")
                            {
                                textBlock1.Text = "";
                                m_res -= float.Parse(m_store[i + 1].ToString());
                                textBlock1.Text = m_res.ToString();
                            }
                            else if (m_store[i].ToString() == "*")
                            {
                                textBlock1.Text = "";
                                m_res *= float.Parse(m_store[i + 1].ToString());
                                textBlock1.Text = m_res.ToString();
                            }
                            else if (m_store[i].ToString() == "/")
                            {
                                textBlock1.Text = "";
                                m_res /= float.Parse(m_store[i + 1].ToString());
                                textBlock1.Text = m_res.ToString();
                            }


                        }
                        m_store.Clear();
                        m_value = textBlock1.Text;

                        for (int i = 0; i < m_value.Length; i++)
                        {
                            if (m_value[i].ToString() == ".")
                            {
                                //bttnDot.Enabled = false;
                                break;
                            }
                            else
                            {
                                //bttnDot.Enabled = true;
                            }
                        }
                        DetailsText.Text += textBlock1.Text + System.Environment.NewLine + "--------" + System.Environment.NewLine;
                        //bttnEqual.Enabled = false;
                    }
                    catch
                    {
                        //MessageBox.Show(exception.Message, "Error");
                    }


                    
                    
                    if ((bool)checkBox2.IsChecked)
                        ShowResult();
                    break;
                case "C":
                    Reset();
                    break;
                default:
                    AddToArray(CurrentButton);
                    
                    break;
            }
        }

        private void AnimateButtonClick(Button sender)
        {
            BTN = ((Button)sender);
            PlaneProjection pp = new PlaneProjection();
            ((Button)sender).Projection = pp;
            Storyboard _storyBoard = new Storyboard();
            DoubleAnimation animationY = new DoubleAnimation()
            {
                From = 0,
                Duration = TimeSpan.FromSeconds(1),
                To = 360,
            };

            Storyboard.SetTarget(animationY, pp);
            Storyboard.SetTargetProperty(animationY, new PropertyPath("RotationY"));
            _storyBoard.Children.Add(animationY);
            _storyBoard.Completed += delegate { sender.Projection = ppo; };
            _storyBoard.Begin();
        }

        private void ShowResult()
        {
            // Creating the Scale Animation of the Main Grid
            Storyboard stbScaleMainGrid = new Storyboard();
            DoubleAnimation daMainGridScaleX = new DoubleAnimation();
            DoubleAnimation daMainGridScaleY = new DoubleAnimation();
            Storyboard.SetTarget(daMainGridScaleX,ContentGrid);
            Storyboard.SetTarget(daMainGridScaleY,ContentGrid);
            Storyboard.SetTargetProperty(daMainGridScaleX, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"));
            Storyboard.SetTargetProperty(daMainGridScaleY, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"));

            stbScaleMainGrid.Children.Add(daMainGridScaleX);
            stbScaleMainGrid.Children.Add(daMainGridScaleY);

            daMainGridScaleX.From = 1;            
            daMainGridScaleX.To = 0.75;
            daMainGridScaleY.From = 1;
            daMainGridScaleY.To = 0.75;

            stbScaleMainGrid.Completed += delegate 
            {
                // Creating the Rotate Animation of Main Grid
                PlaneProjection ppRotateMainGrid = new PlaneProjection();
                ContentGrid.Projection = ppRotateMainGrid;
                Storyboard sbRotateMainGrid = new Storyboard();
                DoubleAnimation animationY = new DoubleAnimation()
                {
                    From = 0,
                    Duration = TimeSpan.FromMilliseconds(500),
                    To = 90
                };
                Storyboard.SetTarget(animationY, ppRotateMainGrid);
                Storyboard.SetTargetProperty(animationY, new PropertyPath("RotationY"));
                sbRotateMainGrid.Children.Add(animationY);
                sbRotateMainGrid.Completed += delegate
                {
                    // Creating the rotate Animation of the Details Grid
                    PlaneProjection ppd = new PlaneProjection();
                    DetailsGrid.Projection = ppd;
                    DetailsGrid.Visibility = System.Windows.Visibility.Visible;
                    Storyboard sbRotateDetailsGridShow = new Storyboard();
                    DoubleAnimation animationYd = new DoubleAnimation()
                    {
                        From = 270,
                        Duration = TimeSpan.FromMilliseconds(500),
                        To = 360,
                    };
                    Storyboard.SetTarget(animationYd, ppd);
                    Storyboard.SetTargetProperty(animationYd, new PropertyPath("RotationY"));
                    sbRotateDetailsGridShow.Children.Add(animationYd);
                    sbRotateDetailsGridShow.Completed += delegate
                    {
                        // Creating the Scale Animation of the Details Grid
                        Storyboard sbScaleDetailsGrid = new Storyboard();
                        DoubleAnimation daDetailsGridScaleX = new DoubleAnimation();
                        DoubleAnimation daDetailsGridScaleY = new DoubleAnimation();
                        Storyboard.SetTarget(daDetailsGridScaleX, DetailsGrid);
                        Storyboard.SetTarget(daDetailsGridScaleY, DetailsGrid);
                        Storyboard.SetTargetProperty(daDetailsGridScaleX, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"));
                        Storyboard.SetTargetProperty(daDetailsGridScaleY, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"));

                        sbScaleDetailsGrid.Children.Add(daDetailsGridScaleX);
                        sbScaleDetailsGrid.Children.Add(daDetailsGridScaleY);

                        daDetailsGridScaleX.From = 0.75;
                        daDetailsGridScaleX.To = 1;
                        daDetailsGridScaleY.From = 0.75;
                        daDetailsGridScaleY.To = 1;
                        sbScaleDetailsGrid.Begin();
                    };

                    sbRotateDetailsGridShow.Begin();
                };
                sbRotateMainGrid.Begin();
            };
            stbScaleMainGrid.Begin();
            isDetails = true;
        }

        private void PhoneApplicationPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (Exitting)
                return;

            if (isDetails)
            {
                e.Cancel = true;
                HideResult();
            }
            else
            {
                e.Cancel = true;
                // Creating the Scale Animation of the Main Grid
                Storyboard sbScaleMainGrid = new Storyboard();
                //DoubleAnimation daMainGridScaleX = new DoubleAnimation();
                DoubleAnimation daMainGridScaleY = new DoubleAnimation();
                //Storyboard.SetTarget(daMainGridScaleX, ContentGrid);
                Storyboard.SetTarget(daMainGridScaleY, ContentGrid);
                //Storyboard.SetTargetProperty(daMainGridScaleX, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"));
                Storyboard.SetTargetProperty(daMainGridScaleY, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"));

                PlaneProjection ppd = new PlaneProjection();
                ContentGrid.Projection = ppd;
                //DoubleAnimation animationYd = new DoubleAnimation()
                //{
                //    From = 0,
                //    Duration = TimeSpan.FromMilliseconds(2000),
                //    To = 360,
                //};
                //Storyboard.SetTarget(animationYd, ppd);
                //Storyboard.SetTargetProperty(animationYd, new PropertyPath("RotationY"));
                //sbScaleMainGrid.Children.Add(animationYd);
                //sbScaleMainGrid.Children.Add(daMainGridScaleX);
                sbScaleMainGrid.Children.Add(daMainGridScaleY);

                //daMainGridScaleX.From = 1;
                //daMainGridScaleX.To = 0;
                daMainGridScaleY.From = 1;
                daMainGridScaleY.To = 0;
                //sbScaleMainGrid.Completed += delegate {(NavigationService.CanGoBack)?NavigationService.GoBack(); };
                sbScaleMainGrid.Begin();
            }
        }

        //private void HideResult()
        //{

            //this.daDetailsGridScaleX.From = 1;
            //this.daDetailsGridScaleX.To = 0.75;
            //this.daDetailsGridScaleY.From = 1;
            //this.daDetailsGridScaleY.To = 0.75;


            //this.daMainGridScaleX.From = 0.75;
            //this.daMainGridScaleY.To = 1;
            //this.daMainGridScaleX.From = 0.75;
            //this.daMainGridScaleY.To = 1;

            //stbScaleDetailsGrid.Completed += delegate(object sender, EventArgs e)
            //{
            //    PlaneProjection ppd = new PlaneProjection();
            //    DetailsGrid.Projection = ppd;
            //    DetailsGrid.Visibility = System.Windows.Visibility.Visible;
            //    Storyboard _storyBoardResultd = new Storyboard();
            //    DoubleAnimation animationYd = new DoubleAnimation()
            //    {
            //        From = 0,
            //        Duration = TimeSpan.FromMilliseconds(500),
            //        To = 90,
            //    };
            //    Storyboard.SetTarget(animationYd, ppd);
            //    Storyboard.SetTargetProperty(animationYd, new PropertyPath("RotationY"));
            //    _storyBoardResultd.Children.Add(animationYd);
            //    _storyBoardResultd.Completed += delegate(object sender2, EventArgs e2)
            //    {
            //        PlaneProjection pp = new PlaneProjection();
            //        ContentGrid.Projection = pp;
            //        Storyboard _storyBoardResult = new Storyboard();
            //        DoubleAnimation animationY = new DoubleAnimation()
            //        {
            //            From = 270,
            //            Duration = TimeSpan.FromMilliseconds(500),
            //            To = 360
            //        };
            //        Storyboard.SetTarget(animationY, pp);
            //        Storyboard.SetTargetProperty(animationY, new PropertyPath("RotationY"));
            //        _storyBoardResult.Children.Add(animationY);
            //        _storyBoardResult.Completed += delegate(object s, EventArgs ee)
            //        {
            //            //stbScaleMainGrid.Begin();
            //        };
            //        _storyBoardResult.Begin();
            //    };
            //    _storyBoardResultd.Begin();
            //};
            //stbScaleDetailsGrid.Begin();


            
            

            //isDetails = false;
        //}

        private void HideResult()
        {

            // Creating the Scale Animation of the Details Grid
            Storyboard sbScaleDetailsGrid = new Storyboard();
            DoubleAnimation daDetailsGridScaleX = new DoubleAnimation();
            DoubleAnimation daDetailsGridScaleY = new DoubleAnimation();
            Storyboard.SetTarget(daDetailsGridScaleX, DetailsGrid);
            Storyboard.SetTarget(daDetailsGridScaleY, DetailsGrid);
            Storyboard.SetTargetProperty(daDetailsGridScaleX, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"));
            Storyboard.SetTargetProperty(daDetailsGridScaleY, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"));

            sbScaleDetailsGrid.Children.Add(daDetailsGridScaleX);
            sbScaleDetailsGrid.Children.Add(daDetailsGridScaleY);

            daDetailsGridScaleX.From = 1;
            daDetailsGridScaleX.To = 0.75;
            daDetailsGridScaleY.From = 1;
            daDetailsGridScaleY.To = 0.75;
            sbScaleDetailsGrid.Completed += delegate
            {
                // Creating the rotate Animation of the Details Grid
                PlaneProjection ppd = new PlaneProjection();
                DetailsGrid.Projection = ppd;
                DetailsGrid.Visibility = System.Windows.Visibility.Visible;
                Storyboard sbRotateDetailsGridShow = new Storyboard();
                DoubleAnimation animationYd = new DoubleAnimation()
                {
                    From = 0,
                    Duration = TimeSpan.FromMilliseconds(500),
                    To = 90,
                };
                Storyboard.SetTarget(animationYd, ppd);
                Storyboard.SetTargetProperty(animationYd, new PropertyPath("RotationY"));
                sbRotateDetailsGridShow.Children.Add(animationYd);
                sbRotateDetailsGridShow.Completed += delegate
                {
                    // Creating the Rotate Animation of Main Grid
                    PlaneProjection ppRotateMainGrid = new PlaneProjection();
                    ContentGrid.Projection = ppRotateMainGrid;
                    Storyboard sbRotateMainGrid = new Storyboard();
                    DoubleAnimation animationY = new DoubleAnimation()
                    {
                        From = 279,
                        Duration = TimeSpan.FromMilliseconds(500),
                        To = 360
                    };
                    Storyboard.SetTarget(animationY, ppRotateMainGrid);
                    Storyboard.SetTargetProperty(animationY, new PropertyPath("RotationY"));
                    sbRotateMainGrid.Children.Add(animationY);
                    sbRotateMainGrid.Completed += delegate
                    {
                        // Creating the Scale Animation of the Main Grid
                        Storyboard sbScaleMainGrid = new Storyboard();
                        DoubleAnimation daMainGridScaleX = new DoubleAnimation();
                        DoubleAnimation daMainGridScaleY = new DoubleAnimation();
                        Storyboard.SetTarget(daMainGridScaleX, ContentGrid);
                        Storyboard.SetTarget(daMainGridScaleY, ContentGrid);
                        Storyboard.SetTargetProperty(daMainGridScaleX, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"));
                        Storyboard.SetTargetProperty(daMainGridScaleY, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"));

                        sbScaleMainGrid.Children.Add(daMainGridScaleX);
                        sbScaleMainGrid.Children.Add(daMainGridScaleY);

                        daMainGridScaleX.From = 0.75;
                        daMainGridScaleX.To = 1;
                        daMainGridScaleY.From = 0.75;
                        daMainGridScaleY.To = 1;

                        sbScaleMainGrid.Begin();
                        
                    };
                    sbRotateMainGrid.Begin();
                };

                sbRotateDetailsGridShow.Begin();
            };
            sbScaleDetailsGrid.Begin();
            isDetails = false;
        }
    }
}